package promote.brozen.base.thread;

import promote.brozen.utils.concurrent.Threads;

/**
 * LockSupport 会使线程进入WAITING状态，但是不会释放锁，第二次打印的T2状态是BLOCKED
 *
 * object.wait 会使线程进入WAITING状态，会释放锁，可以看到打印了T2 get monitor
 *
 * @author Brozen
 * @date 2020/9/11 8:25 AM
 * @email brozen@qq.com
 */
public class LockSupportAndMonitor {

    private static final Object MONITOR = new Object();

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            synchronized (MONITOR) {
                System.out.println("T1 get monitor");
                Threads.silentSleep(1000L);
                // LockSupport.park();

                try {
                    MONITOR.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });


        Thread t2 = new Thread(() -> {
            Threads.silentSleep(500L);
            synchronized (MONITOR) {
                System.out.println("T2 get monitor, T1 state=" + t1.getState());
            }
        });

        t1.start();
        t2.start();
        Threads.silentSleep(100L);

        System.out.println("T1 = " + t1.getState() + " T2 = " + t2.getState());
        Threads.silentSleep(1000L);
        System.out.println("T1 = " + t1.getState() + " T2 = " + t2.getState());

    }

}
